<!--课程与课题-名师直播-->
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>名师直播</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <span>课程名称：<el-input size="small" style="width: 200px;" placeholder="请输入" /></span>
      <el-form-item label="专业等级：">
        <el-select size="small" v-model="formInline.parentcode" placeholder="请选择" @change="search">
          <el-option label="全部" value="ALL"></el-option>
          <el-option label="一级" value="FEIZHI"></el-option>
          <el-option label="二级" value="JIUYI"></el-option>
          <el-option label="三级" value="JINSHU"></el-option>
          <el-option label="四级" value="SULIAO"></el-option>
          <el-option label="五级" value="JIADIAN"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="讲师：">
        <el-select size="small" v-model="formInline.parentcode" placeholder="请选择" @change="search">
          <el-option label="全部" value="ALL"></el-option>
          <el-option label="黄一" value="FEIZHI"></el-option>
          <el-option label="李一" value="JIUYI"></el-option>
          <el-option label="王一" value="JINSHU"></el-option>
          <el-option label="张一" value="SULIAO"></el-option>
          <el-option label="孙一" value="JIADIAN"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态：">
        <el-select size="small" v-model="formInline.parentcode" placeholder="请选择" @change="search">
          <el-option label="审核中" value="FEIZHI"></el-option>
          <el-option label="已发布" value="JIUYI"></el-option>
          <el-option label="下架" value="JINSHU"></el-option>

        </el-select>
      </el-form-item>
      <el-form-item label="操作">
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-search" @click="clan">重置</el-button>

      </el-form-item>
      <br>
      <div class="addBut">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="itemAdd('add')">创建直播课</el-button>
      </div>

    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="itemList" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <!-- <el-table-column align="center" type="selection" width="50">
      </el-table-column> -->
      <el-table-column align="center" sortable prop="bczb_logo_url" label="封面" width="150">
        <template #default="scope">
          <el-image
            style="width: 80px; height: 50px"
            :src="scope.row.bczb_logo_url"
            :preview-src-list="[scope.row.bczb_logo_url]"
            fit="cover"
          >
            <template #error>
              <div class="image-error">加载失败</div>
            </template>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column align="center" sortable prop="bczb_name" label="课程名称" width="250">
      </el-table-column>
      <el-table-column align="center" sortable prop="bczb_starttime" label="开播时间" width="200">
      </el-table-column>
      <el-table-column align="center" sortable prop="bczb_zylevel" label="专业等级" width="250">
      </el-table-column>
      <el-table-column align="center" sortable prop="bctzb_list" label="讲师" width="200">
      </el-table-column>
      <el-table-column align="center" sortable prop="bczb_price" label="价格(元)" width="200">
      </el-table-column>
      <el-table-column align="center" sortable prop="bczb_publishstate" label="状态" width="200">
      </el-table-column>
      <el-table-column align="center" sortable prop="item_unit" label="已购人数" width="200">
      </el-table-column>
      <el-table-column label="操作" min-width="300">
        <template v-slot="scope">
          <el-button size="mini" v-if="scope.row.bczb_publishstate=== '未发布'" @click="updatePublicStatus(scope.$index, scope.row,1)">发布</el-button>
          <el-button size="mini" v-if="scope.row.bczb_publishstate === '已发布'" @click="updatePublicStatus(scope.$index, scope.row,0)">下架</el-button>
          <el-button size="mini" @click="selectStudents(scope.$index, scope.row)">查看学员</el-button>
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑子分类界面 -->
<!--    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>-->
<!--      <el-form label-width="80px" ref="editForm" :model="editForm">-->
<!--        <el-form-item label="子类名称" prop="itemname">-->
<!--          <el-input size="small" v-model="editForm.itemname" auto-complete="off" placeholder="请输入子分类名称"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="描述" prop="unitname">-->
<!--          <el-input size="small" v-model="editForm.unitname" auto-complete="off" placeholder="请输入描述"  type="textarea"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="价格" prop="itemprice">-->
<!--          <el-input size="small" v-model="editForm.itemprice" placeholder="请输入价格"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="单位" prop="itemunit">-->
<!--          <el-input size="small" v-model="editForm.itemunit" placeholder="请输入单位"></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>-->
<!--        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>-->
<!--      </div>-->


<!--    </el-dialog>-->
    <!-- 添加子分类界面 -->
<!--    <el-dialog :title="title" :visible.sync="addFormVisible" width="30%" @click='closeDialog("add")'>-->
<!--      <el-form label-width="80px" ref="editForm" :model="editForm">-->
<!--        <el-form-item label="子类名称" prop="itemname">-->
<!--          <el-input size="small" v-model="editForm.itemname" auto-complete="off" placeholder="请输入子分类名称"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="描述" prop="unitname">-->
<!--          <el-input size="small" v-model="editForm.unitname" auto-complete="off" placeholder="请输入描述"  type="textarea"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="价格" prop="itemprice">-->
<!--          <el-input size="small" v-model="editForm.itemprice" placeholder="请输入价格"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="单位" prop="itemunit">-->
<!--          <el-input size="small" v-model="editForm.itemunit" placeholder="请输入单位"></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button size="small" @click='closeDialog("add")'>取消</el-button>-->
<!--        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitAddForm('editForm')">添加</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
  </div>
</template>

<script>
// 导入请求方法
import Pagination from '../../components/Pagination'
import {deletecbzb, querybtlist, querycbzblist, updatecbzbstate} from "../../api/InstitutionApi";
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      title: '添加分类',
      editFormVisible: false, //控制编辑页面显示与隐藏
      addFormVisible: false,  //控制添加页面显示与隐藏
      dataAccessshow: false, //控制数据权限显示与隐藏
      unitAccessshow: false, //控制所属单位隐藏与显示
      // 编辑与添加
      editForm: {
        itemcode:'',  //子分类ID
        itemname :'',  //子分类名称
        unitname :'', ///分类称重说明
        itemprice :'', ///价格，保留2位小数点
        itemunit :'', ///分类计价单位
      },
      // 请求分类明细数据参数
      formInline: {
        page: 1,
        limit: 10,
        deptId: '',
        userName: '',
        userMobile: '',
        parentcode: ''
      },
      // 课程明细数据列表
      itemList:[],
      teacherList: [], ///讲师数据
      teacherCode:[], ////讲师code
      // 数据权限
      UserDept: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 数据发生改变
   */
  watch: {},

  /**
   * 创建完毕
   */
  created() {
    // 加载讲师列表
    this.getTeacher()
    // 加载直播列表
    this.getdata()
  },
  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 调取讲师名字列表
    getTeacher() {
      querybtlist({}).then(res => {
        // this.tableDate = res.data.base_info;
        console.log('讲师列表：', res);
        // this.tableDate = res.data.data.base_info
        this.teacherList = res.data.data.base_info
      })
    },

    // 直播列表获取
    getdata() {
      this.loading = true
      querycbzblist().then(res => {
        console.log('直播课程：', res)
        let baseinfo = res.data.data.base_info
        this.getBaseInfoData(baseinfo)
        this.loading = false
      })
    },

    // 数据处理
    getBaseInfoData(infoData) {
      this.itemList = []
      for(var i=0;i<infoData.length;i++){// 获取讲师串
        this.itemList.push({
          bczb_banner: infoData[i].bczb_banner,
          bczb_buylasttime: infoData[i].bczb_buylasttime,
          bczb_code: infoData[i].bczb_code,
          bczb_desc: infoData[i].bczb_desc,
          bczb_gdmoney: infoData[i].bczb_gdmoney,
          bczb_limitnum: infoData[i].bczb_limitnum,
          bczb_logo_url: infoData[i].bczb_logo_uri[0].url,
          bczb_logo_uri: infoData[i].bczb_logo_uri[0].imguri,
          bczb_name: infoData[i].bczb_name,
          bczb_percent: infoData[i].bczb_percent,
          bczb_price: infoData[i].bczb_price,
          bczb_publishstate: String(infoData[i].bczb_publishstate) === "0" ? '未发布': '已发布',
          bczb_starttime: infoData[i].bczb_starttime,
          bczb_tuikuanhours: infoData[i].bczb_tuikuanhours,
          bczb_urllink: infoData[i].bczb_urllink,
          bczb_zylevel: infoData[i].bczb_zylevel,
          create_time: infoData[i].create_time,
          bctzb_list: this.getTeacherData(infoData[i].bctzb_list),
          bztzb_jscode: this.teacherCode
        })
      }

    },
    getTeacherData(listString) {
      // 1. 将字符串 list 转换为数组
      const codeList = JSON.parse(listString);
      this.teacherCode = codeList
      // 2. 匹配 code 并获取 name
      const matchedNames = codeList.map(code => {
        const foundItem = this.teacherList.find(item => item.bt_code === code);
        return foundItem ? foundItem.bt_name : null; // 如果没找到，返回 null
      });
      // 3. 过滤掉未匹配的项（可选）
      const validNames = matchedNames.filter(name => name !== null);
      console.log('讲师转化：',validNames);
      return validNames.join('、');
    },

    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    //搜索事件
    search() {
      // console.log('item-formInline:',this.formInline.parentcode)
      this.getdata(this.formInline.parentcode)

    },
    // 重置
    clan() {

    },
    // 添加直播课
    itemAdd() {
      // 跳转页面的三种方式
      // 方式1：通过 path 跳转
      // this.$router.push({
      //   path: `/enterprises/EnEdit/:index`
      // });
      // 方式2：通过 name 跳转（推荐）
      // this.$router.push({
      //   name: 'enedit',
      //   params: { index: index }
      // });
      // 方式3：带查询参数（显示在URL中）
      this.$router.push({
        path: '/onlineCourse/liveCourseEdit',
        query: ''
      });
    },
     // 编辑课程
    handleEdit(index,row) {
      this.$router.push({
        path: '/onlineCourse/liveCourseEdit',
        query: {zbrow: row}
      });
    },

    // // 选择复选框事件
    selectChange(val) {
      this.selectdata = val
    },

    // 查看学员
    selectStudents() {
      this.$router.push({
        path: '/onlineCourse/students',
        query: ''
      })
    },

    // 更新直播状态
    updatePublicStatus(index,row,value) {
      updatecbzbstate({
        "bczbcode": row.bczb_code,  //#直播课的code
        "bczbstate":value, //#直播课程状态，0未发布，1已发布，2已经结束
      }).then(res => {
        console.log('update:', res)
        if (res.msg === '更新直播课程状态信息成功') {
          this.$message({
            type: 'success',
            message: '更新直播课程状态信息成功!'
          })
          this.getdata()
        } else {
          this.$message({
            type: 'info',
            message: res.msg
          })
        }
      })
    },

    // 删除直播
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 删除
          deletecbzb({
            "bczbcode": row.bczb_code, ///#直播课的code
          })
            .then(res => {
              // console.log('delete-then:',res)
              if (res.msg == '删除直播课程信息成功') {
                this.$message({
                  type: 'success',
                  message: '删除直播课程信息成功!'
                })
                this.getdata()
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('数据删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除！'
          })
        })
    },
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.addBut {
  margin-bottom: 10px;
}
.userRole {
  width: 100%;
}
</style>

